package edu.gatech.ealf.ttsplaf;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;

import java.awt.event.ActionListener;
import javax.swing.AbstractButton;

public class TTSButtonUI extends ButtonUI implements MouseListener, ActionListener { 
    
    private TTSHelper helper = null;
    
    public TTSButtonUI() {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new TTSButtonUI();
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        TextToSpeechLookAndFeel.getInstance().registerComponentInstallation(this, c);
        helper = TTSHelper.getInstance();
        AbstractButton a = (AbstractButton)c;
        helper.readConfig(a);
        a.addMouseListener(this);
        a.addActionListener(this);     
    }

    @Override
    public void paint(Graphics g, JComponent c) {

    }

    @Override
    public void uninstallUI(JComponent c) {
    	super.uninstallUI(c);
        TextToSpeechLookAndFeel.getInstance().registerComponentUninstallation(this, c);
        AbstractButton a = (AbstractButton)c;
        a.removeMouseListener(this);
        a.removeActionListener(this);
    }

    @Override
    public void update(Graphics g, JComponent c) {

    }

    public void mouseClicked(MouseEvent e) {
        if(e.getButton()== MouseEvent.BUTTON2){
            helper.toggleSpeak();
        }
    }

    public void mouseEntered(MouseEvent e) {
        helper.mouseEntered(e);
    }
    
    public void mouseExited(MouseEvent e) {
        try {
            helper.mouseExited();
        } catch (InterruptedException ex) {
            Logger.getLogger(TTSButtonUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
        helper.actionPerformed(e);
    }
    
    
    
}
